/*
 * Copyright (C) <2009>  <Matthew Dickinson>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.pulsarwebdevelopment.concordance.plugins;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

import com.pulsarwebdevelopment.concordance.plugins.FileBrowser.FileType;
//import javax.swing.*;

/**
 * Randomly removes words from text file
 * @author Matthew Dickinson
 * Last updated: 11/9/2008
 */
public class TextBlanker implements Plugin{

	private int SPACE_COUNT = 0;
	private int max = 10;
	private int wordsCounted;

	public static void main(String[] args){
		//add files to args[] and call go();
	}

	/**
	 * randomly generates a number for <I>SPACE_COUNT</I>
	 *	picks the smaller of the random number and the predefined <I>max</I>   
	 */
	public void randomCount(){
		//AND ME!
		SPACE_COUNT = (int)(Math.random()*max);
		wordsCounted = 0;
	}

	public void go(Object[] args) {
		//FIX: not blanking after line break
		File file = (File) args[0];
		File exportFile = (File) args[1];
		String maxWords = (String) args[2];
		try
		{
			max = Integer.parseInt(maxWords);
		}
		catch(Exception e){}

		randomCount();
		
		try{
			BufferedReader inFile = new BufferedReader(new FileReader(file));
			BufferedWriter outFile = new BufferedWriter(new FileWriter(exportFile));

			String line;
			String word;
			StringBuffer buildWord = new StringBuffer();
			char[] nextLine;
			
			//inserts line breaks for paragraphs
			outFile.write("\t");
			while((line = inFile.readLine()) != null){
				
				//ADD SOME SORT OF CHECK TO KEEP WORDS WITH NUMBERS FROM BEING DELETED
				
				nextLine = line.toCharArray();

				for(char curChar: nextLine){
					if(Character.isLetter(curChar) || Character.isDigit(curChar)){
						buildWord.append(curChar);
					}else{
						if((curChar == '\'' || curChar ==':') && buildWord.length() > 0){
							buildWord.append(curChar);
						}else{
							if(buildWord.length() > 0){
								word = buildWord.toString();
								buildWord = new StringBuffer();
								
								//verify that this isn't a verse number
								if(word.matches("[1-9].*")){
									System.out.println("NUMBER");
									outFile.write(word);
									outFile.write(curChar);
									continue;
								}

								if(wordsCounted == SPACE_COUNT){
									for(int j = 0; j < word.length(); j++){
										outFile.write("_");
									}
									
									randomCount();
								}else{
									outFile.write(word);
									wordsCounted++;
								}
							}
							
							outFile.write(curChar);
						}
					}
				}
				if(buildWord.length() > 0){
					word = buildWord.toString();
					buildWord = new StringBuffer();
					outFile.write(word);
					//wordsCounted++;
					System.out.println("Line ended without punctuation: " + word);
				}
				outFile.write("\n\t");
			}
			outFile.close();
			inFile.close();

		}catch(Exception e){
			//TODO display error message
			//JOptionPane.showMessageDialog(null, e.getMessage());
			System.exit(0);
		}
		//JOptionPane.showMessageDialog(null, "Done!");
		return;
		
	}

	public void plug(PluginScreen screen) {
		screen.setTitleText("Text Blanker", "Randomly removes words from a document");
		screen.giveFileBrowser(FileBrowser.FileType.TEXT, "Choose file to process.");
		screen.giveFileBrowser(FileBrowser.FileType.TEXT, "Choose file to output results to.");
		screen.giveTextField("Enter the max number of words between blanks");
	}
	public Map<String, Argument> getParameterList(){
		Map<String, Plugin.Argument> args = new HashMap<String, Plugin.Argument>();
		args.put("file", new Plugin.Argument(SelectionType.FileBrowser, new Object[]{FileType.TEXT}, "Choose file to process.",null));
		args.put("newFile", new Plugin.Argument(SelectionType.FileBrowser, new Object[]{FileType.TEXT},"Choose file to output results to.", null));
		args.put("words", new Plugin.Argument(SelectionType.TextInput, null, "Enter the max number of words between blanks",null));
		return args;
	}
	
	public String getTitle(){
		return "Text Blanker";
	}
	
	public String getDescription(){
		return "Randomly removes words from a document";
	}
	
	public void run(Map<String, Argument> args){
		File file = (File) args.get("file").selectedValue;
		File exportFile = (File) args.get("newFile").selectedValue;
		String numWords = (String) args.get("words").selectedValue;
		try
		{
			max = Integer.parseInt(numWords);
		}
		catch(Exception e){}

		randomCount();
		
		try{
			BufferedReader inFile = new BufferedReader(new FileReader(file));
			BufferedWriter outFile = new BufferedWriter(new FileWriter(exportFile));

			String line;
			String word;
			StringBuffer buildWord = new StringBuffer();
			char[] nextLine;
			
			//inserts line breaks for paragraphs
			outFile.write("\t");
			while((line = inFile.readLine()) != null){
				
				//ADD SOME SORT OF CHECK TO KEEP WORDS WITH NUMBERS FROM BEING DELETED
				
				nextLine = line.toCharArray();

				for(char curChar: nextLine){
					if(Character.isLetter(curChar) || Character.isDigit(curChar)){
						buildWord.append(curChar);
					}else{
						if((curChar == '\'' || curChar ==':') && buildWord.length() > 0){
							buildWord.append(curChar);
						}else{
							if(buildWord.length() > 0){
								word = buildWord.toString();
								buildWord = new StringBuffer();
								
								//verify that this isn't a verse number
								if(word.matches("[1-9].*")){
									System.out.println("NUMBER");
									outFile.write(word);
									outFile.write(curChar);
									continue;
								}

								if(wordsCounted == SPACE_COUNT){
									for(int j = 0; j < word.length(); j++){
										outFile.write("_");
									}
									
									randomCount();
								}else{
									outFile.write(word);
									wordsCounted++;
								}
							}
							
							outFile.write(curChar);
						}
					}
				}
				if(buildWord.length() > 0){
					word = buildWord.toString();
					buildWord = new StringBuffer();
					outFile.write(word);
					//wordsCounted++;
					System.out.println("Line ended without punctuation: " + word);
				}
				outFile.write("\n\t");
			}
			outFile.close();
			inFile.close();

		}catch(Exception e){
			//TODO display error message
			//JOptionPane.showMessageDialog(null, e.getMessage());
			System.exit(0);
		}
		//JOptionPane.showMessageDialog(null, "Done!");
		return;
		}

	public boolean requiresCompiledConcordances() {
		return false;
	}
}